<?php

class present_student extends Controller {

	function present_student()
	{
		parent::Controller();	
	}
	
	function index()
	{
		$this->load->model('GoodStudentModel');
		$this->load->model('StudentModel');
	    $data['feedback'] = $this->GoodStudentModel->getFeedBack();
	    
	    $this->load->library('pagination');

	    $per_page = 6;
   		$total = $this->StudentModel->count_all();
   		
   
   		$base_url = site_url('present_student/index/');
	    $config['base_url'] = $base_url;
	    $config['total_rows'] = $total;
	    $config['per_page'] = $per_page;
	    $config['uri_segment'] = '3';
	
	    $this->pagination->initialize($config); 
		
		$data['paging'] = str_replace('index.html/', 'index/', $this->pagination->create_links());

		$this->load->model('filemanagementmodel');
	    $data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
	    $data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
		$data['students'] = $this->StudentModel->getAllLimit($per_page, $this->uri->segment(3));
		
		$this->load->view('f_header', $data);
		$this->load->view('f_present_student', $data);
		$this->load->view('f_footer', $data);
	}
	
function addSchool()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
		$this->load->view('header');
		$this->load->view('school-order-form');
		$this->load->view('footer');
	}
	
	function saveSchool()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $data['order_id'] = $this->input->post('txtSchoolID') ? $this->input->post('txtSchoolID') : null;
	    $data['school_name'] = $this->input->post('txtSchool') ? $this->input->post('txtSchool') : null;
	    $this->load->model('schoolordermodel');
	    if($data['order_id'] == null) {
	    	$seq = $this->schoolordermodel->getMaxSeq();
	    	$data['seq'] = $seq;
	    	$id = $this->schoolordermodel->add($data);
	    } else {
	    	$id = $data['order_id'];
	    	$this->schoolordermodel->update($data);
	    }
	    $d['school'] = $this->schoolordermodel->getByID($id);
		$this->load->view('header');
		$this->load->view('school-order-form', $d);
		$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
		$this->load->view('footer', $msg);
	}
	
	function sortSchool()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('schoolordermodel');
	    $d['data'] = $this->schoolordermodel->getAll();
		$this->load->view('header');
		$this->load->view('school-sort-list-form', $d);
		$this->load->view('footer');
	}
	
	function saveSchoolOrder()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('schoolordermodel');
	    
		$idArr = $this->input->post('sid');
    	$seqArr = $this->input->post('txtSeq');
    	$nnArr = $this->input->post('sc_name');
    	for($i = 0; $i < count($idArr); $i++) {
    		$this->schoolordermodel->updateSeq($idArr[$i], $seqArr[$i], $nnArr[$i]);
    	}
	    
	    $d['data'] = $this->schoolordermodel->getAll();
		$this->load->view('header');
		$this->load->view('school-sort-list-form', $d);
		$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
		$this->load->view('footer', $msg);
	}
	
	function deleteSchoolOrder()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('schoolordermodel');
	    
		$idArr = $this->input->post('del');
    	for($i = 0; $i < count($idArr); $i++) {
    		$this->schoolordermodel->delete($idArr[$i]);
    	}
	    
	    $d['data'] = $this->schoolordermodel->getAll();
		$this->load->view('header');
		$this->load->view('school-sort-list-form', $d);
		$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
		$this->load->view('footer', $msg);
	}
}

?>